package com.listeners;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebListener;

//@WebListener
public class ListenerApplication implements ServletContextListener, ServletContextAttributeListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();//获取被监听对象getServletContext
        System.out.println(servletContext.hashCode()+"ServletContext is created");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();//获取被监听对象getServletContext
        System.out.println(servletContext.hashCode()+"ServletContext is destroyed");

    }

    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        ServletContext servletContext = scae.getServletContext();
        String name = scae.getName();//获取被添加属性名
        String value = scae.getValue().toString();//获取被添加属性值
        System.out.println(servletContext.hashCode()+"ServletContext attribute is added,name:"+name+",value:"+value);
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        ServletContext servletContext = scae.getServletContext();
        String name = scae.getName();//获取被删除属性名
        String value = scae.getValue().toString();//获取被删除属性值
        System.out.println(servletContext.hashCode()+"ServletContext attribute is removed,name:"+name+",value:"+value);
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        ServletContext servletContext = scae.getServletContext();
        String name = scae.getName();//获取被替换属性名
        String value = scae.getValue().toString();//获取被替换掉属性值(是替换前的属性)
        System.out.println(servletContext.hashCode()+"ServletContext attribute is replaced,name:"+name+",value:"+value);
    }
}
